<?php

namespace app\common\model;

use think\Model;

class Activity extends Model
{
    protected $table = "activity";

    protected $createTime = 'created_at';

    protected $updateTime = 'updated_at';

    public static $status = [
    	'going' => '进行中',
    	'stop' => '已停止',
    	'finished' => '已结束', 
    ];

    public function category()
    {
        return $this->belongsTo('ActivityCategory', 'category_id');
    }


    public function scopeTop($query)
    {
    	return $query->where('is_index_top', 1)->order('id desc')->limit(3);
    }

    public function scopeHot($query)
    {
        return $query->where('is_index_recommend', 1)->order('id desc')->limit(2);
    }

    public function scopeFilter($query, $data)
    {
        if(!empty($data))
        {
            if(isset($data['category_id']))
            {
                return $query->where('category_id', $data['category_id']);
            }
        }

        return $query;
    }

    //获取活动类别
    public function getType()
    {
        $type =  $this->getAttr('type');
        $types = $this->category['type'];
        foreach($types as $v)
        {
            if($v['id'] == $type)
            {
                return $v['name'];
            }
        }
        return '';
    }
}
